<template>
  <div>
    <el-form :rules="rules"
             v-loading="loading"
             element-loading-text="正在登录..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)"
             ref="loginForm"
             :model="loginForm"
             class="loginContainer">
      <h3 class="loginTitle">注册用户</h3>

      <el-form-item prop="username">
        <el-input type="text" clearable auto-complete="false" v-model="loginForm.username"
                  placeholder="请输入用户名" @blur="blur">
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" clearable auto-complete="false" v-model="loginForm.password"
                  placeholder="请输入密码">
        </el-input>
      </el-form-item>
      <el-form-item prop="telephone">
        <el-input type="text" clearable auto-complete="false" v-model="loginForm.telephone"
                  placeholder="请输入电话号码">
        </el-input>
      </el-form-item>
      <el-form-item prop="pid">
        <el-input type="text" clearable auto-complete="false" v-model="loginForm.pid"
                  placeholder="请输入身份证号码">
        </el-input>
      </el-form-item>
      <el-form-item prop="address">
        <el-select v-model="loginForm.address" placeholder="请选择地址">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <div class="block">
          <el-date-picker
              v-model="loginForm.birthday"
              type="date"
              placeholder="选择生日日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
      <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        telephone: "",
        address: "",
        birthday: "",
        pid: "",
      },
      options: [
        {
          value: 'Beijing',
          label: '北京'
        },
        {
          value: 'Tianjin',
          label: '天津'
        },
        {
          value: 'Hebei',
          label: '河北'
        },
        {
          value: 'Shanxi',
          label: '山西'
        },
        {
          value: 'Inner Mongolia',
          label: '内蒙古'
        },
        {
          value: 'Liaoning',
          label: '辽宁'
        },
        {
          value: 'Jilin',
          label: '吉林'
        },
        {
          value: 'Heilongjiang',
          label: '黑龙江'
        },
        {
          value: 'Shanghai',
          label: '上海'
        },
        {
          value: 'Jiangsu',
          label: '江苏'
        },
        {
          value: 'Zhejiang',
          label: '浙江'
        },
        {
          value: 'Anhui',
          label: '安徽'
        },
        {
          value: 'Fujian',
          label: '福建'
        },
        {
          value: 'Jiangxi',
          label: '江西'
        },
        {
          value: 'Shandong',
          label: '山东'
        },
        {
          value: 'Henan',
          label: '河南'
        },
        {
          value: 'Hubei',
          label: '湖北'
        },
        {
          value: 'Hunan',
          label: '湖南'
        },
        {
          value: 'Guangdong',
          label: '广东'
        },
        {
          value: 'Guangxi',
          label: '广西'
        },
        {
          value: 'Hainan',
          label: '海南'
        },
        {
          value: 'Chongqing',
          label: '重庆'
        },
        {
          value: 'Sichuan',
          label: '四川'
        },
        {
          value: 'Guizhou',
          label: '贵州'
        },
        {
          value: 'Yunnan',
          label: '云南'
        },
        {
          value: 'Tibet',
          label: '西藏'
        },
        {
          value: 'Shaanxi',
          label: '陕西'
        },
        {
          value: 'Gansu',
          label: '甘肃'
        },
        {
          value: 'Qinghai',
          label: '青海'
        },
        {
          value: 'Ningxia',
          label: '宁夏'
        },
        {
          value: 'Xinjiang',
          label: '新疆'
        },
        {
          value: 'Hong Kong',
          label: '香港'
        },
        {
          value: 'Macau',
          label: '澳门'
        },
        {
          value: 'Taiwan',
          label: '台湾'
        }
      ],

      loading: false,
      checked: true,
      rules: {
        username: [{required: true, message: "请输入用户名", trigger: "blur"}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        telephone: [{required: true, message: '请输入电话', trigger: 'blur'}],
        address: [{required: true, message: '请输入地址', trigger: 'blur'}],
        pid: [{required: true, message: '请输入身份证号码', trigger: 'blur'}],
        birthday: [{required: true, message: '请输入生日', trigger: 'blur'}],
      }
    }
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = '/captcha?time=' + new Date();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;//登录加载动画开启
          this.postRequest('/api/user', this.loginForm).then(resp => {
            if (resp.code === 1) {
              this.loading = false;
              console.log(resp)
              this.$message.success("注册成功")
              this.$router.replace("/")
            } else {
              this.$message.error("账户或密码错误")
            }
          })
        } else {
          this.$message.error('请输入所有字段！');
          return false;
        }
      });
    },
    blur() {
      this.getRequest(`/api/user/name?username=${this.loginForm.username}`).then((resp => {
        if (resp.code != 1) {
          this.$message.error(resp.msg + " , 换一个名字吧");
          this.loginForm.username = "";
        }
      }))
    }
  }

}
</script>

<style scoped>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 100px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #ffffff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 0 auto 40px auto;
  text-align: center;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

</style>
